package cn.easybuy.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.easybuy.dao.BaseDao;
import cn.easybuy.dao.ProductDao;
import cn.easybuy.entity.Product;

public class ProductDaoImpl extends BaseDao implements ProductDao {
	
	Connection conn = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	
	@Override
	public List<Product> getAllProductByCategoryId(int categoryId) {
		// TODO Auto-generated method stub
		String sql ="select * from easybuy_product  "
				+ " where isDelete=0 and categoryLevel1Id=? limit 0,6";
		
		List<Product> list =new ArrayList<Product>();
		
		try {
			Object[] params= {categoryId};
			rs=super.exeQuery(sql, params);
			
			Product product=null;
			while(rs.next()) {
				product =tableToClass(rs);
				list.add(product);
			}
			return list;
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			close(conn, pstmt, rs);
		}
		
		return null;
	}

	@Override
	public Product tableToClass(ResultSet rs) throws Exception {
		// TODO Auto-generated method stub
		Product product = new Product();
        product.setId(rs.getInt("id"));
        product.setName(rs.getString("name"));
        product.setDescription(rs.getString("description"));
        product.setPrice(rs.getFloat("price"));
        product.setStock(rs.getInt("stock"));
        product.setCategoryLevel1Id(rs.getInt("categoryLevel1Id"));
        product.setCategoryLevel2Id(rs.getInt("categoryLevel2Id"));
        product.setCategoryLevel3Id(rs.getInt("categoryLevel3Id"));
        product.setFileName(rs.getString("fileName"));
		return product;
	}

}
